<template>
  <div class="Withdraw">
    <a-card>
      <a-form :form="form">
        <a-row :gutter="30">
          <a-col :span="8">
            <a-form-item
              label="合作方名称"
              :label-col="{ span: 7 }"
              :wrapper-col="{ span: 16,offset:1 }"
            >
              <a-input placeholder="请输入" v-decorator="['workNumber']" />
            </a-form-item>
          </a-col>

          <a-col :span="8">
            <a-form-item label="工单号" :label-col="{ span: 6}" :wrapper-col="{ span: 16, offset: 1}">
              <a-input placeholder="请输入" v-decorator="['workNum']" />
            </a-form-item>
          </a-col>

          <a-col :span="8">
            <a-form-item label="处理状态" :label-col="{ span: 6}" :wrapper-col="{ span: 16, offset: 1}">
              <a-select style="width:100%" v-decorator="['activationStatus']" placeholder="全部">
                <!-- <a-select-option
                        v-for="item in activateSelect"
                        :key="item.id"
                        :value="item.id"
                >{{item.name}}</a-select-option>-->
                <a-select-option value="1">待审核</a-select-option>
                <a-select-option value="2">待打款</a-select-option>
                <a-select-option value="3">待打款成功</a-select-option>
                <a-select-option value="4">已拒绝</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="8">
            <a-form-item label="申请时间" :label-col="{ span: 6}" :wrapper-col="{ span: 16, offset: 1}">
              <a-range-picker
                style="width: 100%"
                v-decorator="['applyTime']"
                show-time
                format="YYYY-MM-DD HH:mm:ss"
              />
            </a-form-item>
          </a-col>
          <a-col :span="16" :style="{ textAlign: 'right' }">
            <a-button type="primary" @click="partsearch()">查询</a-button>
            <a-button type="dashed" style="margin:0 2%" @click="partnerExport()">导出</a-button>
            <a-button @click="handleReset()">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <!-- 表格模块 -->
    <a-card style="margin-top:2%;">
      <a-table
        :columns="columnss"
        :dataSource="dataa"
        :pagination="pagination"
        @change="handleTableChange"
        :rowKey="record => record.createTime"
        :bordered="true"
      >
        <template slot="operationb" slot-scope="text,record">
          <a
            href="javascript:;"
            style="color:rgb(0,214,206)"
            @click="pass(text,record)"
          >{{record.status!==1?"":"通过"}}</a>
          <a-divider type="vertical" v-show="record.status!==1?'':'通过'" />
          <a
            href="javascript:;"
            style="color:red"
            @click="refuse(text,record)"
          >{{record.status!==1?"":"拒绝"}}</a>
        </template>
        <template slot="status" slot-scope="text,record">
          <span>{{ record.status==1?"待审核":record.status==2?"审核通过":record.status==3?"打款成功":record.status==4?"审核未通过":''}}</span>
        </template>
      </a-table>
    </a-card>
    <a-modal
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      v-model="visible"
      :closable="false"
      :width="400"
    >
      <a-textarea
        style="width:100%;height:230px"
        placeholder="请输入拒绝原因"
        v-model="reason"
        :autosize="{ minRows: 2, maxRows: 4, }"
      />
    </a-modal>
  </div>
</template>

<script>
const columnss = [
  {
    title: "时间",
    dataIndex: "createTime",
    scopedSlots: { customRender: "createTime" },
    align: "center"
  },
  {
    title: "工单号",
    dataIndex: "workOrderCode",
    scopedSlots: { customRender: "workOrderCode" },
    align: "center"
  },
  {
    title: "合作方名称",
    dataIndex: "partnerName",
    scopedSlots: { customRender: "partnerName" },
    align: "center"
  },
  {
    title: "处理状态",
    dataIndex: "status",
    scopedSlots: { customRender: "status" },
    align: "center"
  },
  {
    title: "收款账户",
    dataIndex: "receivingAccount",
    scopedSlots: { customRender: "receivingAccount" },
    align: "center"
  },
  {
    title: "提现总金额",
    dataIndex: "workOrderAmount",
    scopedSlots: { customRender: "workOrderAmount" },
    align: "center"
  },
  {
    title: "提现手续费",
    dataIndex: "serviceCharge",
    scopedSlots: { customRender: "serviceCharge" },
    align: "center"
  },
  {
    title: "操作",
    dataIndex: "",
    scopedSlots: { customRender: "operationb" },
    align: "center"
  }
];

const dataa = [
  {
    key: "1",
    // orderPaymentTime: "2019/02/09",
    // workOrderGeneratedTime: "2019/02/10 10:51:03",
    // workOrderNumber: "9049766422641879466",
    // processingCompletionTime: "2019/02/10 10:51:03",
    // agent: "代理商",
    // entryValueSum: "12.45",
    // wechatAmount: "6.56",
    // alipayAmount: "5.89",
    // headquartersFenRun: "1.28",
    // partnersShareProfits: "0.56",
    operation: "详情"
  }
];

export default {
  name: "Withdraw",
  data() {
    return {
      form: this.$form.createForm(this),
      columnss,
      dataa: [],
      pagination: {
        showSizeChanger: true,
        showQuickJumper: true,
        pageSizeOptions: ["10", "20", "30", "40"], //每页条数选择器
        showTotal: (total, range) => {
          return `${range[0]}-${range[1]} 共${total}条`;
        }, // 总数展示
        pageSize: 10, //每页条数
        defaultCurrent: 1, //默认当前页
        current: 1, //当前页
        total: 50 //数据总条数
      },
      visible: false,
      reason: ""
    };
  },
  watch: {},
  computed: {},
  methods: {
    handleReset() {
      this.form.resetFields();
    },
    pass(text, record) {
      var that = this;
      this.withdrawCashId = record.id;
      this.$confirm({
        title: "审核通过",
        content: `确定要通过 ${record.partnerName} 的这次提现申请吗？`,
        onOk() {
          that.auditWithDrawCash(that.withdrawCashId);
        },
        onCancel() {}
      });
    },
    refuse(text, record) {
      this.visible = true;
      this.refuseCashId = record.id;
    },
    handleOk(e) {
      this.visible = false;
      this.refuseCash();
    },
    handleCancel(e) {
      this.visible = true;
    },

    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination };
      pager.current = pagination.current;
      pager.pageSize = pagination.pageSize;
      pager.total = pagination.total;
      this.pagination = pager;
      this.handleReset();
      this.partsearch();
    },

    //合作方提现查询
    partsearch() {
      var values = this.form.getFieldsValue();
      //对时间进行判断一下
      if (values.applyTime == undefined) {
        values.applyTime == undefined;
      } else {
        this.startTime = this.$moment(values.applyTime[0]._d).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.endTime = this.$moment(values.applyTime[1]._d).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      this.axios
        .post(this.baseUrl + "/partnerFinancial/findWithDrawCashPageList", {
          loginToken: this.$store.state.loginToken,
          endTime: this.endTime,
          startTime: this.startTime,
          pageNum: this.pagination.current,
          pageSize: this.pagination.pageSize,
          workOrderCode: values.workNum
        })
        .then(response => {
          // console.log(response.data.data.t);
          this.dataa = response.data.data.t.list;
          //可提现金额
          this.accountBalance = response.data.data.t.accountBalance;
          //锁定金额
          this.lockAmount = response.data.data.t.lockAmount;
          //  合作方收入总金额
          this.partnerInAmount = response.data.data.t.partnerInAmount;
          //已提现金额
          this.settledAmount = response.data.data.t.settledAmount;
          this.pagination.total = response.data.data.total;
          this.pagination.pageSize = response.data.data.pageSize;
          this.pagination.current = response.data.data.pageNum;
        });
    },
    //合作方提现导出
    partnerExport() {
      this.axios
        .post(
          this.baseUrl + "/partnerFinancial/exportWithDrawCashList",
          {
            loginToken: this.$store.state.loginToken
          },
          { responseType: "arraybuffer" }
        )
        .then(response => {
          // console.log(response);
          if (response.status == "200") {
            const aLink = document.createElement("a");
            let blob = new Blob([response.data], {
              type: "application/vnd.ms-excel"
            });
            aLink.href = URL.createObjectURL(blob);
            aLink.download = "合作方提现表";
            aLink.click();
            document.body.appendChild(aLink);
          }
        });
    },

    // 合作方提现审核--通过
    auditWithDrawCash(withdrawCashId) {
      this.axios
        .post(this.baseUrl + "/partnerFinancial/auditWithDrawCash", {
          loginToken: this.$store.state.loginToken,
          authorType: 2,
          status: 1,
          withdrawCashId: withdrawCashId
        })
        .then(response => {
          if (response.data.code == 0) {
            this.$message.success("审核成功");
          }
        });
    },

    // 合作方提现审核--拒绝
    refuseCash() {
      var values = this.form.getFieldsValue();
      this.axios
        .post(this.baseUrl + "/partnerFinancial/auditWithDrawCash", {
          loginToken: this.$store.state.loginToken,
          authorType: 2,
          reason: this.reason,
          status: 1,
          withdrawCashId: this.refuseCashId
        })
        .then(response => {
          if (response.data.code == 0) {
            this.$message.success("已拒绝申请");
          }
        });
    }
  },
  created() {
    this.partsearch();
    // this.partnerSearch();
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.Withdraw {
  width: 100%;
}
</style>
